package com.warwithinme.android.widgetwar;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;

public class ButtonAutoRotate extends TButton
{
	
	private final static String INTENT_ACTION = "com.warwithinme.android.widgetwar.ButtonAutoRotate";

	@Override
	public int canHandleIntent(Intent intent)
	{
		if(intent.getAction().equals(INTENT_ACTION))
			return ToggleWidget.HANDLING_TYPE_IMMEDIATE;
		else
			return ToggleWidget.HANDLING_TYPE_UNABLE;
	}

	@Override
	public int getButtonBmp(Context context)
	{
		int autoRotate = Settings.System.getInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION,0);
		if(autoRotate == 1)
			return R.drawable.autorotate_on;
		else
			return R.drawable.autorotate_off;
	}

	@Override
	public String getIntentAction()
	{
		return INTENT_ACTION;
	}

	@Override
	public void toggleSetting(Context context)
	{
		ContentResolver cr = context.getContentResolver();
		int autoRotate = Settings.System.getInt(cr, Settings.System.ACCELEROMETER_ROTATION,0);
		if(autoRotate == 1)
			Settings.System.putInt(cr, Settings.System.ACCELEROMETER_ROTATION, 0);
		else
			Settings.System.putInt(cr, Settings.System.ACCELEROMETER_ROTATION, 1);
	}

}
